import pandas as pd
# 测试数据
df = pd.DataFrame({'category_col': ['A','B','A','C']*250000})
# 优化前内存
original_memory = df.memory_usage(deep=True).sum()
# 转换为category类型
df['category_col'] = df['category_col'].astype('category')
# 验证内存节省
optimized_memory = df.memory_usage(deep=True).sum()
assert optimized_memory < original_memory * 0.3  # 通常节省70%以上内存